package com.zhuoli.service.springboot.mybatis.config.controller;

import com.zhuoli.service.springboot.mybatis.config.repository.model.User;
import com.zhuoli.service.springboot.mybatis.config.service.UserControllerService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: zhuoli
 * @Date: 2018/8/2 11:17
 * @Description:
 */
@RestController
@AllArgsConstructor
@RequestMapping(value = "/user")
@Slf4j
public class UserController {

    private UserControllerService userControllerService;

    @RequestMapping(value = "/get_all", method = RequestMethod.POST)
    public ResponseEntity getAllUser() {
        return ResponseEntity.status(HttpStatus.OK).body(userControllerService.getAllUsers());
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ResponseEntity createUser(@RequestParam(value = "userName") String userName,
                                     @RequestParam(value = "description") String description) {
        User user = new User();
        user.setUserName(userName);
        user.setDescription(description);

        userControllerService.createUser(user);
        return ResponseEntity.status(HttpStatus.OK).body("create user success");
    }

    @RequestMapping(value = "/query_user", method = RequestMethod.POST)
    public ResponseEntity queryUserById(@RequestParam(value = "id") Long id) {
        return ResponseEntity.status(HttpStatus.OK).body(userControllerService.getUserById(id));
    }

    @RequestMapping(value = "/update_user", method = RequestMethod.POST)
    public ResponseEntity updateUserById(@RequestParam(value = "id") Long id,
                                         @RequestParam(value = "userName") String userName,
                                         @RequestParam(value = "description") String description) {
        User user = new User();
        user.setId(id);
        user.setUserName(userName);
        user.setDescription(description);
        userControllerService.updateUser(user);
        return ResponseEntity.status(HttpStatus.OK).body("update user success");
    }

    @RequestMapping(value = "/del_user", method = RequestMethod.POST)
    public ResponseEntity deleteUserById(@RequestParam(value = "id") Long id) {
        userControllerService.delUserById(id);
        return ResponseEntity.status(HttpStatus.OK).body("del user success");
    }
}
